package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"weekly-exam-practice/models"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	author := c.PostForm("author")
	if title == "" || content == "" || author == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数不能为空",
		})
		return
	}
	article := models.Article{
		Title:   title,
		Content: content,
		Author:  author,
	}
	err := models.DB.Create(&article).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	var article models.Article
	err := models.DB.Find(&article).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "列表信息获取失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "列表信息获取成功成功",
		"data": article,
	})
	return
}
